//app.js
const app = getApp()
App({
  // data: {
  //   canIUse: wx.canIUse('button.open-type.getUserInfo')
  // },
  onLaunch: function () {
    if (!wx.getStorageSync('user')) {
      wx.getSetting({
        success:(res)=> {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
            wx.login({
              success:(res)=> {
                if (res.code) {
                  wx.getUserInfo({
                    withCredentials: true,
                    success: (res_user) => {
                      wx.request({
                        url: 'https://m.frenchiesplus.com/Public/getOpenId',
                        data: {
                          code: res.code
                        },
                        method: 'POST',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: (data) =>{
                          if (data.data.success == 1) {
                            wx.request({
                              //后台接口地址
                              url: 'https://m.frenchiesplus.com/Public/login',
                              data: {
                                wx_openid: data.data.data.openid,
                                wx_headimgurl: res_user.userInfo.avatarUrl,
                                wx_nickname: res_user.userInfo.nickName
                              },
                              method: 'POST',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded'
                              },
                              success: (res) => {
                                if (res.data.success == 1) {
                                  this.globalData.userInfo = res.data.data;
                                  let user = {
                                    "openId": res.data.data.openId,
                                    "user_token": res.data.data.user_token
                                  }
                                  wx.setStorageSync("user", JSON.stringify(user))
                                  wx.redirectTo({
                                    url: '/pages/index/index'
                                  })
                                } else {
                                  wx.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 2000
                                  })
                                }
                              }
                            })
                          } else {
                            wx.showToast({
                              title: res.data.msg,
                              icon: 'none',
                              duration: 2000
                            })
                          }

                        }
                      })
                    }, fail: function () {
                    }, complete: function (res) {
                    }
                  })
                }
              }
            })
          }else{
            wx.reLaunch({
              url: '/pages/logs/logs',
            })
          }
        }
      })
   
    }else{
      wx.request({//检查过期
        url: 'https://m.frenchiesplus.com/Users/userInfo',
        data: {
          'user_token': JSON.parse(wx.getStorageSync("user")).user_token
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success:  (res)=> {
          if (res.data.success == '1'){//token有效
            let timer = setTimeout(()=>{
              clearTimeout(timer)
              // wx.redirectTo({
              //   url: '/pages/index/index'
              // })
            },1000)
            
          }else{//token 过期
            wx.login({
              success: (res) => {
                if (res.code) {
                  wx.getUserInfo({
                    withCredentials: true,
                    success: (res_user) => {
                      console.log(res_user)
                      wx.request({
                        url: 'https://m.frenchiesplus.com/Public/getOpenId',
                        data: {
                          code: res.code
                        },
                        method: 'POST',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: (data) => {
                          if (data.data.success == 1) {
                            wx.request({
                              //后台接口地址
                              url: 'https://m.frenchiesplus.com/Public/login',
                              data: {
                                wx_openid: data.data.data.openid,
                                wx_headimgurl: res_user.userInfo.avatarUrl,
                                wx_nickname: res_user.userInfo.nickName
                              },
                              method: 'POST',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded'
                              },
                              success: (res) => {
                                if (res.data.success == 1) {
                                  this.globalData.userInfo = res.data.data;
                                  let user = {
                                    "openId": res.data.data.openId,
                                    "user_token": res.data.data.user_token
                                  }
                                  wx.setStorageSync("user", JSON.stringify(user))
                                  // wx.redirectTo({
                                  //   url: '/pages/index/index'
                                  // })
                                } else {
                                  wx.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 2000
                                  })
                                }
                              }
                            })
                          } else {
                            wx.showToast({
                              title: res.data.msg,
                              icon: 'none',
                              duration: 2000
                            })
                          }

                        }
                      })
                    }, fail: function () {
                    }, complete: function (res) {
                    }
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  globalData: {
    userInfo: null,
    base_url: 'https://m.frenchiesplus.com'
  },
  
})